package com.ruoyi.notice.controller;

import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.notice.service.FyMoneyLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 23118
 * @version 1.0
 * @description: TODO
 * @date 2025/5/17 15:36
 */
@RequestMapping("/fyMoneyLog")
@RestController
public class FyMoneyLogController {
    @Autowired
    private FyMoneyLogService fyMoneyLogService;

    /**
     * 获取用户的资金变动日志
     * @param userId
     * @return
     */
    @GetMapping("/getFyMoneyLog")
    public AjaxResult getFyMoneyLog(@RequestParam("userId") Long userId) {
        List<Map> fyMoneyLogs = fyMoneyLogService.getFyMoneyLog(userId);
        return AjaxResult.success(fyMoneyLogs);
    }

    /**
     * 获取当前用户余额
     */
    @GetMapping("/getBalance")
    public AjaxResult getBalance(@RequestParam("userId") Long userId) {
        double balance = fyMoneyLogService.getBalance(userId);
        return AjaxResult.success(balance);
    }

    /**
     * 获取用户累计收入和累计提现以及余额
     */
    @GetMapping("getCountMoney")
    public AjaxResult getCountMoney(@RequestParam("userId") Long userId) {
        double balance = fyMoneyLogService.getBalance(userId);
        double totalIncome = fyMoneyLogService.getTotalIncome(userId);
        double totalWithdrawal = fyMoneyLogService.getTotalWithdrawal(userId);
        Map<String, Object> result = new HashMap<>();
        result.put("balance", balance);
        result.put("totalIncome", totalIncome);
        result.put("totalWithdrawal", totalWithdrawal);
        return AjaxResult.success(result);
    }
    /**
     * 提现记录
     */
    @GetMapping("/getWithdrawal")
    public AjaxResult getWithdrawal(@RequestParam("userId") Long userId) {
        List<Map> withdrawal = fyMoneyLogService.getWithdrawal(userId);
        return AjaxResult.success(withdrawal);
    }

    /**
     * 提现申请
     */
    @PostMapping("/withdrawal")
    public AjaxResult withdrawal(@RequestBody Map map) {
        int i = fyMoneyLogService.addWithdrawal(map);
        if (i == 0) {
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }
}
